<?php

namespace App\Http\Controllers\Guard\Advert;

use App\Http\Controllers\Common\GuardController;
use App\Models\Advert\Advert;
use App\Models\Constant\Constant;
use App\Models\Constant\UploadConstant;

use App\Models\School\School;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class AdvertController extends GuardController {

    public function lst(Request $request) {

        $type = $request->input('selectType','all');
        $status = $request->input('status');
        $pageSize = $request->input('pageSize',Constant::PAGE_SIZE);
        $page  = $request->input('page',1);
        if($type==0){
            $conditions = compact('status');
        }else{
            $conditions = compact('type','status');
        }


        $records = (new Advert())->getLst($conditions)->paginate($pageSize,$this->getCol,'page',$page);



        $mapType = [
            ['value'=>0,'label'=>'全部'],
            ['value'=>-1,'label'=>'首页'],
            ['value'=>-2,'label'=>'语言中心'],
            ['value'=>-3,'label'=>'留学服务'],
            ['value'=>-4,'label'=>'免考留学'],
            ['value'=>-5,'label'=>'关于我们'],
        ];
        $schools = School::get(['id as value','name as label']);
        foreach ($schools as $school) {
            $mapType[] = ['value'=>$school->value,'label'=>$school->label];
        }

        foreach ($records as $record) {

            foreach ($mapType as $item) {
                if($item['value']==$record->type){
                    $record->type = $item['label'];
                }
            }
            //$record->type =  ??'未知';
            $record->pic_url = asset($record->pic_url);
            $record->link = Str::limit($record->link,50);
            $record->title = Str::limit($record->title,50);
        }

        return responseSuccess($records->items(),$records->total(),100000,['map_type'=>$mapType]);
    }

    public function add(Request $request) {
        $id = $request->input('id',0);
        $info = $request->input();

        //只是为了修改sort字段
        if(count($info)<=2 && isset($info['sort'])){
            Advert::updateOrCreate(compact('id'),$info);
            return responseSuccess();
        }

        $this->validate($request, [
            'pic_url'=>'required',
            //'title'=>'required|min:1|max:200',
        ],[],[
            'pic_url'=>'图片或者视频',
            //'title'=>'标题'
        ]);

        /* $pic_url = $request->input('picUrl');
        $type = $request->input('type');
        $title = $request->input('title');
        $link = $request->input('link');
        $status = $request->has('status')?1:2; */
        //dd($info);
        Advert::updateOrCreate(compact('id'),$info);
        return responseSuccess();
    }

    public function detail(Request $request) {
        $id = $request->input('id',0);
        $records = Advert::find($id);
        $records->url = asset($records->pic_url);
        return responseSuccess([$records->toArray()]);
    }

    public function del(Request $request) {
        //return $this->delRecords(Advert::class, $request->input('id',''));
        $ids = $request->input('ids',[]);
        if(!is_array($ids)){
            $ids = [$ids];
        }
        //Advert::whereIn('id',$ids)->forceDelete();
        Advert::whereIn('id',$ids)->delete();
        return responseSuccess();
    }

    public function status(Request $request) {
        $ids = $request->input('ids',[]);
        $status = $request->input('status',1);
        Advert::whereIn('id',$ids)->update(compact('status'));
        return responseSuccess();
    }

    public function top(Request $request) {
        $ids = $request->input('ids',[]);
        $is_top = $request->input('is_top',1);
        Advert::whereIn('id',$ids)->update(compact('is_top'));
        return responseSuccess();
    }


}
